﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IRepository.LY;
using DFGK.IService.LY;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models.LyModel;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service.LY
{
    public class PurchaseOrderService : BaseService,IPurchaseOrderService
    {
        private readonly IPurchaseOrderRepository _purchaseOrderRepository;
    
        private readonly IUnitOfWork unitOfWork;
        public PurchaseOrderService(IMapper mapper, IPurchaseOrderRepository purchaseOrderRepository, IUnitOfWork unitOfWork) : base(mapper)
        {
            _purchaseOrderRepository = purchaseOrderRepository;
          
            this.unitOfWork = unitOfWork;
           
        }

       

        public List<PurchaseOrder> GetPurchaseOrderData(out int totalCount, int pageIndex = 1, int pageSize = 15)
        {
            var list = _purchaseOrderRepository.GetEntityAlls();
            totalCount = (int)list.Count();
            totalCount = list.Count();
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).OrderByDescending(d => d.Id);
            return list.ToList();
        }
        public bool DelPurchaseOrder(long purchaseOrderId)
        {
            var Quote = _purchaseOrderRepository.QueryEntityById(purchaseOrderId);
            if (Quote == null)
            {
                return false;
            }
            _purchaseOrderRepository.DelEntity(Quote);
            return unitOfWork.SaveChangesEntity();
        }
        public PurchaseOrder? GetPurchaseOrder(long purchaseOrderId)
        {
            return _purchaseOrderRepository.QueryEntityById(purchaseOrderId);
        }

        public bool PurchaseOrderData(PurchaseOrder model, out string errorMsg)
        {
            errorMsg = string.Empty;
            if (model.Id > 0)
            {
                _purchaseOrderRepository.UpdEntity(model);
            }
            else
            {
                _purchaseOrderRepository.AddEntity(model);

            }
            return unitOfWork.SaveChangesEntity();
        }
    }
}
